﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class bookTypeManage : Form
    {
        public bookTypeManage()
        {
            InitializeComponent();
        }

        private void clearTextBox()
        {
            txtBookTypeCode.Text = "";
            txtBookTypeName.Text = "";
            txtBookTypeExplain.Text = "";
            //txtCurrentCode.Text = "";
            //txtParentCode.Text = "";
        }

        private void setTextBoxState(Boolean bState)
        {
            txtBookTypeCode.ReadOnly = bState;
            txtBookTypeName.ReadOnly = bState;
            txtBookTypeExplain.ReadOnly = bState;
            txtCurrentCode.ReadOnly = bState;
            txtParentCode.ReadOnly = bState;
            txtCurrentSelect.ReadOnly = bState;
        }

        private void setButtonState(Boolean bState)
        {
            btnAddEql.Enabled = bState;
            btnAddSub.Enabled = bState;
            btnModify.Enabled = bState;
            btnDelete.Enabled = bState;
            btnSave.Enabled = !bState;
            btnCancel.Enabled = !bState;
        }

        private void initializeTree()
        {
            this.trvList.Nodes.Clear();
            AddTree("0",(TreeNode)null);
            //inintializeText();
        }

        private void inintializeText()
        {
            DataTable dt = bookDbClass.ExecuteDataTable("select 图书类型代码,图书类型名称,图书类型说明,类型层次编号,类型父项编号 from 图书类型 order by 图书类型代码");
            DataView dvList = new DataView(dt);
            this.txtBookTypeName.DataBindings.Clear();
            this.txtBookTypeCode.DataBindings.Clear();
            this.txtBookTypeExplain.DataBindings.Clear();
            this.txtCurrentCode.DataBindings.Clear();
            this.txtCurrentSelect.DataBindings.Clear();
            this.txtParentCode.DataBindings.Clear();
            this.txtBookTypeCode.DataBindings.Add("text", dvList, "图书类型代码");
            this.txtBookTypeName.DataBindings.Add("text", dvList, "图书类型名称");
            this.txtBookTypeExplain.DataBindings.Add("text", dvList, "图书类型说明");
            this.txtCurrentCode.DataBindings.Add("text", dvList, "类型层次编号");
            this.txtParentCode.DataBindings.Add("text", dvList, "类型父项编号");
            this.txtCurrentSelect.DataBindings.Add("text", dvList, "图书类型代码");
        }

        
        private void AddTree(string ParentId, TreeNode Pnode)
        {
            DataTable dt = getBookType();
            DataView dvList = new DataView(dt);
            dvList.RowFilter = "类型父项编号=" + ParentId;
            foreach(DataRowView dr in dvList)
            {
                TreeNode node = new TreeNode();
                if (Pnode == null)
                {
                    string s = (dr["图书类型代码"].ToString() + ',' + dr["图书类型名称"].ToString() + ',' + dr["图书类型说明"].ToString() + ',' + dr["类型层次编号"].ToString() + ',' + dr["类型父项编号"].ToString() + ',' + dr["图书类型代码"].ToString() + "-" + dr["图书类型名称"].ToString());
                    node.Text = dr["图书类型代码"].ToString() + "-" + dr["图书类型名称"].ToString();
                    node.Tag = s;
                    this.trvList.Nodes.Add(node);
                    AddTree(dr["类型层次编号"].ToString(),node);
                }
                else
                {
                    string s = (dr["图书类型代码"].ToString() + ',' + dr["图书类型名称"].ToString() + ',' + dr["图书类型说明"].ToString() + ',' + dr["类型层次编号"].ToString() + ',' + dr["类型父项编号"].ToString() + ',' + dr["图书类型代码"].ToString() + "-" + dr["图书类型名称"].ToString());
                    node.Text = dr["图书类型代码"].ToString() + "-" + dr["图书类型名称"].ToString();
                    node.Tag = s;
                    Pnode.Nodes.Add(node);
                    AddTree(dr["类型层次编号"].ToString(),node);
                }
            }
        }

        
        private static DataTable getBookType()
        {
            DataTable dt = bookDbClass.ExecuteDataTable("select 图书类型代码,图书类型名称,图书类型说明,类型层次编号,类型父项编号 from 图书类型 order by 图书类型代码");
            return dt;
        }

       /* private Boolean saveForAdd(string bookTypeCode, string bookTypeName, string bookTypeExplain, string currentCode, string parentCode)
        {
            try
            {
                string strsql = "insert into 图书类型(图书类型代码,图书类型名称,图书类型说明,类型层次编号,类型父项编号) values({0},{1},{2},{3},{4})";
                strsql = string.Format(strsql, bookTypeCode, bookTypeName, bookTypeExplain, currentCode, parentCode);
                return bookDbClass.insertBySql(strsql);
            }
            catch (Exception)
            {
                MessageBox.Show("数据格式出错！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }*/

      /*  private Boolean saveForEdit(string bookTypeCode,string bookTypeName,string bookTypeExplain,string currentCode,string parentCode)
        {
            //try
            //{
            //    string strsql = "update 图书类型 set 图书类型名称={0},图书类型说明={1},类型层次编号={2},类型父项编号={3} where 图书类型代码={4}";
            //    strsql = string.Format(strsql,bookTypeName, bookTypeExplain, currentCode, parentCode,bookTypeCode);
            //    bookDbClass.ExecuteNonQurey(strsql);
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    //MessageBox.Show("数据格式错误！", "提示信息");
            //    return false;
            //}
        }*/

        private void deleteData(string bookTypeCode)
        {
            string strsql = "delete from 图书类型 where 图书类型代码={0}";
            strsql = string.Format(strsql,bookTypeCode);
            bookDbClass.deleteBySql(strsql);
        }

        private void bookTypeManage_Load(object sender, EventArgs e)
        {
            initializeTree();
            setButtonState(true);
            setTextBoxState(true);
        }

        public int i;
        private void trvList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int i = this.trvList.SelectedNode.Index;

            string s = e.Node.Tag.ToString();
            string[] strs = s.Split(',');
            string str1 = strs[0];
            string str2 = strs[1];
            string str3 = strs[2];
            string str4 = strs[3];
            string str5 = strs[4];
            string str6 = strs[5];
            this.txtBookTypeCode.Text = str1;
            this.txtBookTypeName.Text = str2;
            this.txtBookTypeExplain.Text = str3;
            this.txtCurrentCode.Text = str4;
            this.txtParentCode.Text = str5;
            this.txtCurrentSelect.Text = str6;       
        }

        public string strFlag;
        private void btnAddEql_Click(object sender, EventArgs e)
        {
            strFlag = "isAdd";
            clearTextBox();
            setButtonState(false);
            setTextBoxState(false);
            txtParentCode.ReadOnly = true;
            txtCurrentSelect.ReadOnly = true;
            txtCurrentCode.Text = txtParentCode.Text;
            txtBookTypeCode.Focus();

        }

        private void btnAddSub_Click(object sender, EventArgs e)
        {
            strFlag = "isAdd";
            clearTextBox();
            setButtonState(false);
            setTextBoxState(false);
            txtParentCode.ReadOnly = true;
            txtCurrentSelect.ReadOnly = true;
            txtParentCode.Text = txtCurrentCode.Text;
            txtBookTypeCode.Focus();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            strFlag = "isModify";
            setTextBoxState(false);
            setButtonState(false);
            txtBookTypeCode.Enabled = false;
            txtBookTypeName.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (strFlag == "isAdd")
            {
                if (this.trvList.SelectedNode!=null)
                {
                    //if (saveForAdd(txtBookTypeCode.Text.Trim(), txtBookTypeName.Text.Trim(), txtBookTypeExplain.Text.Trim(), txtCurrentCode.Text.Trim(), txtParentCode.Text.Trim()) == true)
                    //{
                    //    this.trvList.Nodes.Clear();
                    //    this.trvList.Nodes.Add("图书类型");
                    //    initializeTree();
                    //    //this.trvList.Nodes[0].Expand();
                    //}
                    try
                    {
                        if(this.txtCurrentCode.Text==this.txtParentCode.Text|this.txtCurrentCode.Text.Substring(0,this.txtParentCode.Text.Length-1)!=this.txtParentCode.Text)
                        {
                            MessageBox.Show("数据格式错误！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            
                        }
                        else
                        {
                            bookDbClass.ExecuteNonQurey("insert into 图书类型(图书类型代码,图书类型名称,图书类型说明,类型层次编号,类型父项编号) values(@a,@b,@c,@d,@e)", new SqlParameter("a", txtBookTypeCode.Text.Trim().ToString()), new SqlParameter("b", txtBookTypeName.Text.Trim().ToString()), new SqlParameter("c", txtBookTypeExplain.Text.Trim().ToString()), new SqlParameter("d", txtCurrentCode.Text.Trim().ToString()), new SqlParameter("e", txtParentCode.Text.Trim().ToString()));
                        }
                    }
                   catch(Exception)
                    {
                        MessageBox.Show("数据格式错误！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        
                    }
                    this.trvList.Nodes.Clear();
                    this.trvList.Nodes.Add("图书类型");
                    initializeTree();
                    this.trvList.Nodes[0].Expand();
                }
            }
            else
            {
                if (this.trvList.SelectedNode != null)
                {
                    //if (saveForEdit(txtBookTypeCode.Text.Trim(), txtBookTypeName.Text.Trim(), txtBookTypeExplain.Text.Trim(), txtCurrentCode.Text.Trim(), txtParentCode.Text.Trim()) == true)
                    //{
                    //    this.trvList.Nodes.Clear();
                    //    this.trvList.Nodes.Add("图书类型");
                    //    initializeTree();
                    //    this.trvList.Nodes[0].Expand();
                    //}
                    try
                    {
                        if (this.txtCurrentCode.Text== this.txtParentCode.Text)
                        {
                            MessageBox.Show("数据格式错误！123", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        //if (this.txtCurrentCode.Text.Substring(0, this.txtParentCode.Text.Length - 1) != this.txtParentCode.Text)
                        //{
                        //    MessageBox.Show("数据格式错误！1234", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //}
                        else
                        {
                            bookDbClass.ExecuteNonQurey("update 图书类型 set 图书类型名称=@b,图书类型说明=@c,类型层次编号=@d,类型父项编号=@e where 图书类型代码=@a", new SqlParameter("b", txtBookTypeName.Text.Trim().ToString()), new SqlParameter("c", txtBookTypeExplain.Text.Trim().ToString()), new SqlParameter("d", txtCurrentCode.Text.Trim().ToString()), new SqlParameter("e", txtParentCode.Text.Trim().ToString()), new SqlParameter("a", txtBookTypeCode.Text.Trim().ToString()));         
                        }
                    }
                    catch
                    {
                        MessageBox.Show("数据格式错误！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        
                    }
                    this.trvList.Nodes.Clear();
                    this.trvList.Nodes.Add("图书类型");
                    initializeTree();
                    this.trvList.Nodes[0].Expand();
                }
            }
            setButtonState(true);
            setTextBoxState(true);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.trvList.SelectedNode != null)
            {
                if (this.trvList.SelectedNode.Nodes.Count != 0)
                {
                    MessageBox.Show("请先删除该项的全部子项", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (MessageBox.Show("你确认删除此数据吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        //deleteData(txtBookTypeCode.Text.Trim());
                        //this.trvList.SelectedNode.Remove();
                        //clearTextBox();
                     
                        bookDbClass.ExecuteNonQurey("delete from 图书类型 where 图书类型代码=@a",new SqlParameter("a",txtBookTypeCode.Text.Trim()));
                        clearTextBox();
                        this.trvList.Nodes.Clear(); 
                        this.trvList.Nodes.Add("图书类型");
                        initializeTree();
                        this.trvList.Nodes[0].Expand();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择需要删除的节点","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            setButtonState(true);
            setTextBoxState(true);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}